package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.entity.Products;
import com.project.myPackage.service.FavoritesService;
import com.project.myPackage.service.ProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "商品管理")
@Slf4j
@RestController
@RequestMapping("/")
public class ProductsController {
    @Autowired
    private ProductsService productsService;

    @ApiOperation(value = "根据用户id收藏获取推荐商品信息")
    @RequestMapping("/getRecommendProductsByUserId")
    public R<?> getRecommendProductsByUserId(@RequestParam Integer user_id){
        List list = null;
        if(user_id!=null){
         list = productsService.getRecommendProductsByUserId(user_id);
        }

        // 如果推荐商品列表不满五项，则从上架的商品列表中补齐
        int num =6;
        if (list.size() < num) {
            LambdaQueryWrapper<Products> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Products::getStatus, 1);
            List<Products> list1 = productsService.list(queryWrapper);

            // 去除已经存在的推荐商品
            list1.removeAll(list);

            // 补齐推荐商品列表到五项
            int additionalItemsNeeded = num - list.size();
            list.addAll(list1.subList(0, Math.min(additionalItemsNeeded, list1.size())));
        }

        // 截取数组前五项
        list = list.subList(0, Math.min(list.size(), num));
        return R.success(list, "获取推荐商品信息成功！");

    }



    @ApiOperation(value = "条件获取商品信息")
    @RequestMapping("/getAllProducts")
    public R<?> getAllProducts(
            @RequestParam(required = false) Integer category_id,
            @RequestParam(required = false) String product_name,
            @RequestParam(required = false) Integer status) {
        List<Products> list = productsService.listByConditions(category_id, product_name, status);

        if (list != null) {
            return R.success(list, "获取商品列表成功！");
        } else {
            return R.error("获取商品列表失败！");
        }
    }

    @ApiOperation(value = "新增商品信息")
    @PostMapping("/addProducts")
    public R<?> addProducts(@RequestBody Products products) {
        boolean save = productsService.save(products);
        if (save) {
            return R.success("新增商品成功！");
        } else {
            return R.error("新增商品失败！");
        }
    }

    @ApiOperation(value = "删除商品信息")
    @RequestMapping("/deleteProducts")
    public R<?> deleteProducts(@RequestParam Integer product_id) {
        boolean remove = productsService.removeById(product_id);
        if (remove) {
            return R.success("删除商品成功！");
        } else {
            return R.error("删除商品失败！");
        }
    }

    @ApiOperation(value = "修改商品信息")
    @PostMapping("/updateProducts")
    public R<?> updateProducts(@RequestBody Products products) {
        boolean update = productsService.updateById(products);
        if (update) {
            return R.success("修改商品成功！");
        } else {
            return R.error("修改商品失败！");
        }
    }
}
